package Multithread.Thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 5 volatile的使用
 *
 */
@Slf4j
public class Thread05 {
    static volatile boolean flag = true;


    public static void main(String[] args) {

//        5.1 测试volatile
//        fun01();
        log.info("ss");


    }

    public static void fun01() {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t ----- t1 come in");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            flag = false;
        }, "t1").start();

        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t -----t2 come in");
            while (flag) {
                System.out.println(Thread.currentThread().getName() + "\t -----flag目前为ture，循环中");
                try {
                    TimeUnit.MILLISECONDS.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println(Thread.currentThread().getName() + "\t -----flag被设置为false，程序停止");
        }, "t2").start();
    }
}